package com.itheima.service.impl;


import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import com.itheima.domain.Code;
import com.itheima.exception.BusinessException;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;
    @Override
    public boolean save(Book book) {
        bookDao.save(book);
        return true;
    }

    @Override
    public boolean update(Book book) {
        bookDao.update(book);
        return true;
    }

    @Override
    public Book getById(Integer id) {
        //在getById演示触发异常，其他方法省略没有写进来
        //模拟业务异常，包装成自定义异常
        if(id==1){
            throw new BusinessException("请停止当前不正当行为", Code.BUSINESS_ERR);
        }
 /*       try {
            int i=1/0;
        } catch (Exception e) {
            throw new SystemException("服务器访问超时，请重试",Code.SYSTEM_TIMEOUT_ERR);
        }*/
        return bookDao.getById(id);
    }

    @Override
    public List<Book> getAll() {
        return bookDao.getAll();
    }

    @Override
    public boolean delete(Integer id) {
        bookDao.delete(id);
        return true;
    }

}
